const $request = require('superagent');
const { hefeng } = require('../config');

/**
 *@param {
 *  url: string,
 *  method: string,
 *  params:object,
 *  data: object
 * }
 * 和风天气开发文档：https://dev.heweather.com/docs/api/weather
 */
const request = (param) => {
  let { baseApi, key } = hefeng
  param.url = `${baseApi}${param.url[0] === '/' ? param.url : '/' + param.url}`
  param.params = Object.assign(param.params, { key })

  return new Promise((resolve, resject) => {
    $request(param.method || 'GET', param.url).query(param.params).send(param.data).then(response => {
      resolve(response.body)
    }).catch(error => {
      console.log('请求出错：', error)
      resject(error)
    })
  })
}

module.exports = {
  request
}
